package com.lbb.waimai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbb.waimai.entiry.Category;
import com.lbb.waimai.entiry.Food;
import com.lbb.waimai.entiry.vo.FoodVo;
import com.lbb.waimai.mapper.CategoryMapper;
import com.lbb.waimai.mapper.FoodMapper;
import com.lbb.waimai.service.CategoryService;
import com.lbb.waimai.service.FoodService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Autowired
    private CategoryService categoryService;

    @Override
    public List<Food> getFoodListByCategoryId(Integer id) {
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        wrapper.eq("c_id", id);
        List<Food> foodList = baseMapper.selectList(wrapper);
        return foodList;
    }

    /* 查询所有菜品 */
    @Override
    public List<FoodVo> findAllFood() {
        List<Food> foodList = baseMapper.selectList(new QueryWrapper<>());
        List<FoodVo> foodVoList = new ArrayList<>();
        //封装菜品分类
        for (Food food : foodList) {
            Integer cId = food.getCId();
            Category category = categoryService.getById(cId);
            FoodVo foodVo = new FoodVo();
            BeanUtils.copyProperties(food, foodVo);
            foodVo.setCName(category.getCName());
            foodVoList.add(foodVo);
        }
        return foodVoList;
    }
}
